package com.xiaoshuidi.cloud.module.rooms.repository;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaoshuidi.cloud.module.rooms.controller.admin.rentalspace.vo.RentalSpaceExportReqVO;
import com.xiaoshuidi.cloud.module.rooms.mapper.RentalSpaceMapper;
import com.xiaoshuidi.cloud.module.rooms.pojo.RoomRentalSpace;
import com.xiaoshuidi.cloud.module.rooms.repository.irepository.IRentalSpaceRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@RequiredArgsConstructor
public class RentalSpaceRepository extends ServiceImpl<RentalSpaceMapper, RoomRentalSpace> implements IRentalSpaceRepository {

    private final RentalSpaceMapper rentalSpaceMapper;

    @Override
    public List<RoomRentalSpace> exportRentalSpace(RentalSpaceExportReqVO exportReqVO) {

        LambdaQueryWrapper<RoomRentalSpace> wrapper = new LambdaQueryWrapper<>();
        //查询字段
        wrapper.select(
                RoomRentalSpace::getName,
                RoomRentalSpace::getRentStatus,
                RoomRentalSpace::getMonthRental);
        //公寓id
        wrapper.eq(ObjectUtil.isNotEmpty(exportReqVO.getApartmentId()), RoomRentalSpace::getApartmentId, exportReqVO.getApartmentId());

        //类型 商铺、办公室、其他
        if (ObjectUtil.isNotEmpty(exportReqVO.getType())) {
            wrapper.eq(RoomRentalSpace::getType, exportReqVO.getType());
        }

        return rentalSpaceMapper.selectList(wrapper);
    }

    @Override
    public List<RoomRentalSpace> getByspaceNo(String spaceNo) {
        LambdaQueryWrapper<RoomRentalSpace> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RoomRentalSpace::getSpaceNo, spaceNo);
        return rentalSpaceMapper.selectList(wrapper);

    }

    /**
     * 空间定价列表
     *
     * @param apartmentId
     * @param name
     * @param type
     * @param pageSize
     * @param pageNo
     * @return
     */
    public Page<RoomRentalSpace> pricingPage(Long apartmentId, String name, String type, Integer pageSize, Integer pageNo) {
        Page<RoomRentalSpace> page = Page.of(pageNo, pageSize);
        LambdaQueryWrapper<RoomRentalSpace> wrapper = Wrappers.lambdaQuery();
        wrapper.select(RoomRentalSpace::getId, RoomRentalSpace::getName, RoomRentalSpace::getMonthRental, RoomRentalSpace::getApartmentId, RoomRentalSpace::getRentStatus)
                .eq(apartmentId != null, RoomRentalSpace::getApartmentId, apartmentId)
                .like(StrUtil.isNotEmpty(name), RoomRentalSpace::getName, name)
                .eq(StrUtil.isNotEmpty(type), RoomRentalSpace::getType, type)
                .orderByDesc(RoomRentalSpace::getId);
        return page(page, wrapper);
    }

    public Long pricingCount(Long apartmentId, String name, String type) {
        LambdaQueryWrapper<RoomRentalSpace> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(RoomRentalSpace::getApartmentId, apartmentId)
                .like(StrUtil.isNotEmpty(name), RoomRentalSpace::getName, name)
                .eq(StrUtil.isNotEmpty(type), RoomRentalSpace::getType, type)
                .orderByDesc(RoomRentalSpace::getId);
        return count(wrapper);
    }
}
